package com.info.zhiduoduo.admin.service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.info.zhiduoduo.common.constants.SmartResult;
import com.info.zhiduoduo.common.constants.StatusMsg;
import com.info.zhiduoduo.common.exception.Asserts;
import com.info.zhiduoduo.common.exception.SmartException;
import com.info.zhiduoduo.common.formbean.BaseSimpleFormBean;
import com.info.zhiduoduo.common.utils.EasyExcelUtil;
import com.info.zhiduoduo.common.utils.RandomUtil;
import com.info.zhiduoduo.core.service.CoreOddJobService;
import com.info.zhiduoduo.repository.dto.crowdsource.*;
import com.info.zhiduoduo.repository.entity.crowdsource.OddJobMerchant;
import com.info.zhiduoduo.repository.entity.crowdsource.OddJobRechargeRecord;
import com.info.zhiduoduo.repository.mapper.crowdsource.OddJobMerchantBalanceMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.OddJobMerchantFlowMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.OddJobMerchantMapper;
import com.info.zhiduoduo.repository.mapper.crowdsource.OddJobRechargeRecordMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author tianyonghong
 * @create 2023/2/23
 */
@Slf4j
@Service
public class OddJobService {

    @Resource
    OddJobMerchantBalanceMapper oddJobMerchantBalanceMapper;

    @Resource
    OddJobMerchantMapper oddJobMerchantMapper;

    @Resource
    OddJobMerchantFlowMapper oddJobMerchantFlowMapper;

    @Resource
    OddJobRechargeRecordMapper oddJobRechargeRecordMapper;

    @Resource
    CoreOddJobService coreOddJobService;

    //新增充值申请
    public SmartResult addOrUpdateOddJobRechargeRecord(BaseSimpleFormBean<OddJobRechargeRecordAddDto> formBean) {
        OddJobRechargeRecordAddDto addDto = formBean.getReqdata();
        OddJobRechargeRecord info = new OddJobRechargeRecord();
        BeanUtils.copyProperties(addDto, info);
        boolean result = true;
        if (info.getId() == null) {
            //新增
            info.setFlowNo(RandomUtil.createRandomNo("ZLG", ""));
            info.setState(0);
            info.setCreatedBy(formBean.getUserid());
            result = info.insert();
        } else {
            //修改
            info.setUpdatedBy(formBean.getUserid());
            result = info.updateById();
        }
        if (!result) {
            return SmartResult.fail();
        }
        return SmartResult.success();
    }

    //完成充值记录
    @Transactional
    public SmartResult completeOddJobRechargeRecord(BaseSimpleFormBean<OddJobRechargeRecordUpdateDto> formBean) {
        OddJobRechargeRecordUpdateDto updateDto = formBean.getReqdata();
        OddJobRechargeRecord rechargeRecord = oddJobRechargeRecordMapper.selectById(updateDto.getId());
        Asserts.notNull(rechargeRecord);
        rechargeRecord.setState(1);
        rechargeRecord.setUpdatedBy(formBean.getUserid());
        if (!rechargeRecord.updateById()) {
            return SmartResult.fail();
        }
        //生成流水并充值
        return coreOddJobService.rechargeMerchantAndCreateFlowByRecharged(rechargeRecord);
    }

    //查询充值记录列表
    public SmartResult queryOddJobRechargeRecordList(BaseSimpleFormBean<OddJobRechargeRecordQueryDto> formBean){
        OddJobRechargeRecordQueryDto queryDto = formBean.getReqdata();
        Page<OddJobRechargeRecordDetailDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        oddJobRechargeRecordMapper.selectList(queryDto.getMerchantId(),queryDto.getState(),queryDto.getStartTime(),queryDto.getEndTime());
        return SmartResult.ofPage(page);
    }

    //删除充值记录
    public SmartResult delOddJobRechargeRecord(BaseSimpleFormBean<OddJobRechargeRecordQueryDto> formBean){
        OddJobRechargeRecordQueryDto queryDto = formBean.getReqdata();
        Asserts.notNull(queryDto.getId(), StatusMsg.StatusMsg_101);
        OddJobRechargeRecord rechargeRecord = new OddJobRechargeRecord().selectById(queryDto.getId());
        Asserts.notNull(rechargeRecord, StatusMsg.StatusMsg_102);
        rechargeRecord.setIsDel(1).updateById();
        return SmartResult.success();
    }

    //查询商户列表
    public SmartResult queryOddJobMerchantList(BaseSimpleFormBean formBean){
        List<OddJobMerchant> list = oddJobMerchantMapper.selectList(Condition.create());
        return SmartResult.success(list);
    }

    //查询商户流水列表
    public SmartResult queryOddJobMerchantFlowList(BaseSimpleFormBean<OddJobMerchantFlowQueryDto> formBean){
        OddJobMerchantFlowQueryDto queryDto = formBean.getReqdata();
        Page<OddJobRechargeRecordDetailDto> page = PageHelper.startPage(formBean.getPage(), formBean.getRows());
        oddJobMerchantFlowMapper.selectFlowList(queryDto.getMerchantId(),queryDto.getPartyId(),queryDto.getStartTime(),queryDto.getEndTime());
        return SmartResult.ofPage(page);
    }

    public void exportQTemplateUser(HttpServletResponse response, Integer projectId) {
        List<ExportOddJobQTemplateUserDto> dtos = oddJobMerchantFlowMapper.selectQExportUserList(projectId);
        if (CollectionUtils.isEmpty(dtos)) {
            throw new SmartException(StatusMsg.StatusMsg_102);
        }
        String fileName = "《智零工批量上传用户名单》_" + System.currentTimeMillis() + ".xlsx";
        EasyExcelUtil.download(response, fileName,ExportOddJobQTemplateUserDto.class, dtos);
    }

    public void exportQTemplateTask(HttpServletResponse response, Integer projectId) {
        List<ExportOddJobQTemplateTaskDto> dtos = oddJobMerchantFlowMapper.selectQExportTaskList(projectId);
        if (CollectionUtils.isEmpty(dtos)) {
            throw new SmartException(StatusMsg.StatusMsg_102);
        }
        //获取模板
        String fileName = "《智零工批量任务众包名单》_" + System.currentTimeMillis() + ".xlsx";
        EasyExcelUtil.download(response, fileName,ExportOddJobQTemplateTaskDto.class, dtos);
    }
}
